﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class CMSV2_FrontPageImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.QueryString["action"] == "Save")
        {
            ReviewerMySql SQL = new ReviewerMySql();
            SQL.ExecuteNonQuery("UPDATE V2_ForsidePost SET Aktiv = 'True', BilledID = '" + Request.QueryString["PicID"].ToString() + "' WHERE ID = " + Request.QueryString["id"].ToString());
            lblStatus.Text = "<br />Billedet blev gemt";
        }
    }
    protected void btmSave_Click(object sender, EventArgs e)
    {
        if (FileUpTopPic.HasFile)
        {
            string Path = Server.MapPath("../FrontBilled/");
            FileUpTopPic.SaveAs(@Path + FileUpTopPic.FileName.ToString());
        }
        ReviewerMySql SQL = new ReviewerMySql();
        SQL.ExecuteNonQuery("INSERT INTO V2_ForsidePostImg(Filnavn, Tags) VALUES('" + FileUpTopPic.FileName.ToString() + "','" + txtTags.Text + "')");
        OdbcDataReader MyReader = SQL.ExecuteReader("SELECT * FROM V2_ForsidePostImg ORDER BY id DESC LIMIT 1");
        MyReader.Read();
        SQL.ExecuteNonQuery("UPDATE V2_ForsidePost SET Aktiv = 'True', BilledID = '" + MyReader["id"].ToString() + "' WHERE ID = " + Request.QueryString["id"].ToString());
        lblStatus.Text = "<br />Billedet blev gemt";
    }
    protected void btmSearch_Click(object sender, EventArgs e)
    {
        DivResults.InnerHtml = "<b>Klik på Billed for at vælge det til forside posten:</b><br />";
        ReviewerMySql SQL = new ReviewerMySql();
        OdbcDataReader MyReader = SQL.ExecuteReader("SELECT * FROM V2_ForsidePostImg WHERE Tags LIKE '%" + txtSeaarch.Text + "%'");
        int i = 0;
        while (MyReader.Read())
        {
            if (i == 5)
            {
                DivResults.InnerHtml += "<br />";
                i = 0;
            }
            DivResults.InnerHtml += "<a href=\"?action=Save&id=" + Request.QueryString["id"].ToString() +"&PicID=" + MyReader["id"].ToString() + "\"><img border=\"1\" src=\"../FrontBilled/" + MyReader["Filnavn"].ToString() + "\"></a>";
            i++;
        }
    }
}
